using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace XProject.GUI
{
    public partial class PasswordChange : Form
    {
        DAO.AccountDAO daoAccount = new XProject.DAO.AccountDAO();
        public PasswordChange()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (IsValidInput())
            {
                Entity.Account acc = new XProject.Entity.Account();
                acc.Username = txtUsername.Text;
                acc.Password = Util.Encryptor.MD5Encrypt(txtPassword.Text);
                if (daoAccount.GetAccount(acc))
                {
                    if (daoAccount.ChangePassword(new Entity.Account(txtUsername.Text, Util.Encryptor.MD5Encrypt(txtNewPassword.Text))) > 0)
                    {
                        MessageBox.Show("Password has been changed successfully.");
                        this.Dispose();
                    }
                    else MessageBox.Show(daoAccount.Message, "Error");
                }
                else MessageBox.Show("Invalid username or password.");
            }
        }
        public bool IsValidInput()
        {
            if (txtNewPassword.Text != txtConfirmPassword.Text)
            {
                MessageBox.Show("Password and confirm password are not the same");
                return false;
            }
            else if (txtNewPassword.Text == "")
            {
                MessageBox.Show("Password can not be left blank");
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
           
        }
    }
}